package com.yuanqunsoft.alimigration.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.yuanqunsoft.alimigration.bean.AlertBean;
import com.yuanqunsoft.alimigration.dao.IAlertDao;
import com.yuanqunsoft.alimigration.dao.support.Page;
import com.yuanqunsoft.alimigration.model.Alert;
import com.yuanqunsoft.alimigration.service.IAlertService;

public class AlertServiceImpl implements IAlertService {

    private IAlertDao alertDao;

    public IAlertDao getAlertDao() {
        return alertDao;
    }

    public void setAlertDao(IAlertDao alertDao) {
        this.alertDao = alertDao;
    }

    @Override
    public boolean addAlert(Alert instance) {
        try{
            this.alertDao.save(instance);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    @Override
    public boolean deleteAlert(Alert instance) {
        try{
            this.alertDao.delete(instance);
            return true;
        }catch(Exception e){
            return false;
        }
    }

    @Override
    public List<?> findByReportId(int reportId) {
        return this.toAlertBean(this.getAlertDao().findByReportId(reportId));
    }

    @Override
    public List<Alert> Query(String filters, String orders, Object... values) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Page<Alert> pageQuery(int startNo, int pageSize, String filters, String orders, Object... values) {
        // TODO Auto-generated method stub
        return null;
    }

    public AlertBean toAlertBean(Alert alert) {
        AlertBean ab = new AlertBean();
        ab.setAleLine(alert.getAleLine());
        ab.setAleResource(alert.getAleResource());
        ab.setAlertId(alert.getAlertId());
        if (alert.getModule() != null) {
            ab.setModId(alert.getModule().getModId());
            ab.setModName(alert.getModule().getModName());
        }
        if (alert.getRule() != null) {
            ab.setRulId(alert.getRule().getRulId());
            ab.setRulName(alert.getRule().getRulName());
            ab.setRulCategory(alert.getRule().getRulCategory());
            ab.setRulSeverity(alert.getRule().getRulSeverity());
            ab.setRulDescription(alert.getRule().getRulDescription());
        }
        if (alert.getReport() != null) {
            ab.setRepId(alert.getReport().getRepId());
        }
        return ab;
    }
    
    public List<AlertBean> toAlertBean(List<Alert> alertList){
        List<AlertBean> abl = new ArrayList<AlertBean>();
        for(Alert a : alertList){
            abl.add(this.toAlertBean(a));
        }
        return abl;
    }

}
